<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="400"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="角色等级编号" prop="memberRoleLevel">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.memberRoleLevel"
          placeholder="请输入角色等级编号"
          clearable
        />

        <span v-else>{{ formData.memberRoleLevel }}</span>
      </el-form-item>
      <el-form-item label="成员角色名称" prop="memberRoleName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.memberRoleName"
          placeholder="请输入成员角色名称"
          clearable
        />

        <span v-else>{{ formData.memberRoleName }}</span>
      </el-form-item>
      <el-form-item label="管理权限" prop="isAdmin">
        <el-radio-group v-model="formData.isAdmin" v-if="!props.isReadonly">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
        <span v-else>{{ formData.isAdmin == 1 ? "是" : "否" }}</span>
      </el-form-item>

      <el-form-item label="是否启用" prop="enabled">
        <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
        <span v-else>{{ formData.enabled == 1 ? "是" : "否" }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMEMBERROLEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  memberRoleLevel: [
    { required: true, message: "请输入角色等级编号", trigger: "blur" },
    { pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur" },
    { min: 1, max: 10, message: "长度不能超过10个字符", trigger: "blur" },
  ],
  memberRoleName: [
    { required: true, message: "请输入成员角色名称", trigger: "blur" },
  ],
  isAdmin: [
    { required: true, message: "请输入是否有管理权限", trigger: "blur" },
  ],
  enabled: [{ required: true, message: "请输入是否启用", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  memberRoleLevel: "",
  memberRoleName: "",
  isAdmin: 0,
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: 1,
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        memberRoleLevel,
        memberRoleName,
        isAdmin,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        memberRoleLevel,
        memberRoleName,
        isAdmin,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
      formData.memberRoleLevel = String(memberRoleLevel);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.memberRoleLevel = "";
  formData.memberRoleName = "";
  formData.isAdmin = 0;
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = 1;
  formData.deleted = "";
}

const loading = ref(false);

/** 成员角色保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesmemberrole/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 成员角色查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mes/mesmemberrole/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
